<template>
  <el-dialog
    :before-close="close_Change"
    title="用户管理"
    v-model="dialogVisible"
    width="60%"
  >
    <el-container style="height: 500px; border: 1px solid #eee">
      <el-aside width="220px" style="margin-top: 5px">
        <el-button-group>
          <el-button
            type="success"
            @click="submit_form('post')"
            size="mini"
            icon="add-location"
            style="margin-left: 5px"
          ></el-button>
          <el-button
            type="primary"
            @click="submit_form('put')"
            icon="edit"
            size="mini"
          ></el-button>
          <el-button
            type="danger"
            @click="open_del_user_dialog"
            size="mini"
            icon="delete"
          ></el-button>
          <el-button
            @click="get_resource_users"
            size="mini"
            icon="refresh-left"
          ></el-button>
        </el-button-group>
        <el-table
          :cell-style="{ padding: 0 }"
          ref="singleTable"
          :data="resource_data"
          highlight-current-row
          @current-change="handleCurrentChange"
          style="font-size: 10px; width: 100%"
        >
          <el-table-column
            property="resource_user_name"
            show-overflow-tooltip
            label="用户名"
            width="80"
          >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            property="resource_user_permission_text"
            label="角色"
            width="80"
          >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            property="resource_user_comment"
            label="备注"
            width="50"
          >
          </el-table-column>
        </el-table>
      </el-aside>
      <el-main>
        <el-form
          ref="user_from_ref"
          :model="user_from"
          label-width="80px"
          size="mini"
          :rules="rules"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="用户名" prop="resource_user_name">
                <el-input v-model="user_from.resource_user_name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用户类型" prop="conf_manage_code">
                <SysCodeInput
                  sys_code_type="resource_user_permission_type"
                  @childClick="resource_user_permission_CodeClick"
                  :sys_code_value="user_from.resource_user_permission_code"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="密码" prop="resource_user_pass">
                <el-input v-model="user_from.resource_user_pass"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="默认用户">
                <el-switch
                  v-model="user_from.resource_user_default"
                ></el-switch>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="权限key" prop="resource_user_keyfile">
            <el-input
              type="textarea"
              :rows="8"
              placeholder="未开发"
              :disabled="true"
              v-model="user_from.resource_user_keyfile"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="备注" prop="resource_user_comment">
            <el-input
              type="textarea"
              :rows="8"
              placeholder="请输入"
              v-model="user_from.resource_user_comment"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close_Change" size="mini">取 消</el-button>
      </span>
    </template>
  </el-dialog>

  <ConfirmationBox
    text="确认删除选中行"
    @childClick="del_resource_users_info"
    :in_value="open_del_user_dialog_value"
  >
  </ConfirmationBox>
</template>

<script>
import * as Request from "@/general/request";
import SysCodeInput from "@/components/communal/sys_code_input.vue";
import { ElMessage } from "element-plus";
import { watch, ref } from "vue";
import ConfirmationBox from "@/components/communal//confirmation_box.vue";

export default {
  name: "conf_dialog",
  emits: ["childClick"],
  props: {
    resource_user_ids: Number,
    in_value: Boolean,
  },
  setup(props, { emit }) {
    let dialogVisible = ref(props.in_value);

    let user_from = ref({
      resource_user_default: false,
    });
    let resource_data = ref([]);
    let submit_form = ref([]);
    let check_resource_user_id = ref("");
    let open_del_user_dialog_value = ref(false);

    const close_Change = () => {
      dialogVisible.value = false;
      emit("childClick");
    };

    watch(props, (new_val) => {
      dialogVisible.value = new_val.in_value;
      if (new_val.in_value) {
        get_resource_users();
      }
    });

    const open_del_user_dialog = () => {
      if (!check_resource_user_id.value) {
        ElMessage.warning("请选择删除的用户");
        return;
      }
      open_del_user_dialog_value.value = true;
    };

    const handleCurrentChange = (val) => {
      check_resource_user_id.value = val.resource_user_id;
      get_resource_users_info();
    };

    submit_form = (type) => {
      if (type === "post") {
        post_resource_users_info();
      } else if (type === "put") {
        put_resource_users_info();
      }
    };

    const resource_user_permission_CodeClick = (e) => {
      user_from.value.resource_user_permission_code = e;
    };

    const get_resource_users = async () => {
      const response = await Request.GET(
        "/background/home/resource_management/resource_users",
        { resource_user_ids: props.resource_user_ids }
      );
      if (response && response.data) {
        var return_data = response.data;
        if (return_data.success) {
          resource_data.value = return_data.data;
          ElMessage.success(return_data.msg);
        } else {
          ElMessage.error(return_data.msg);
        }
      }
    };

    const get_resource_users_info = async () => {
      user_from.value.resource_user_ids = props.resource_user_ids;
      const response = await Request.GET(
        "/background/home/resource_management/resource_users_info",
        { resource_user_id: check_resource_user_id.value }
      );
      if (response && response.data) {
        var return_data = response.data;
        if (return_data.success) {
          user_from.value = return_data.data;
          ElMessage.success(return_data.msg);
        } else {
          ElMessage.error(return_data.msg);
        }
      }
    };

    const post_resource_users_info = async () => {
      user_from.value.resource_user_ids = props.resource_user_ids;
      const response = await Request.POST(
        "/background/home/resource_management/resource_users_info",
        user_from.value
      );
      if (response && response.data) {
        var return_data = response.data;
        if (return_data.success) {
          await get_resource_users();
          ElMessage.success(return_data.msg);
        } else {
          ElMessage.error(return_data.msg);
        }
      }
    };

    const put_resource_users_info = async () => {
      user_from.value.resource_user_ids = props.resource_user_ids;
      const response = await Request.PUT(
        "/background/home/resource_management/resource_users_info",
        user_from.value
      );
      if (response && response.data) {
        var return_data = response.data;
        if (return_data.success) {
          await get_resource_users();
          ElMessage.success(return_data.msg);
        } else {
          ElMessage.error(return_data.msg);
        }
      }
    };

    const del_resource_users_info = async (signal) => {
      open_del_user_dialog_value.value = false;

      if (!signal) {
        return;
      }

      const response = await Request.DELETE(
        "/background/home/resource_management/resource_users_info",
        { resource_user_id: check_resource_user_id.value }
      );
      if (response && response.data) {
        var return_data = response.data;
        if (return_data.success) {
          await get_resource_users();
          ElMessage.success(return_data.msg);
        } else {
          ElMessage.error(return_data.msg);
        }
      }
    };

    return {
      dialogVisible,
      close_Change,
      get_resource_users,
      props,
      user_from,
      resource_data,
      get_resource_users_info,
      post_resource_users_info,
      submit_form,
      resource_user_permission_CodeClick,
      handleCurrentChange,
      put_resource_users_info,
      del_resource_users_info,
      open_del_user_dialog_value,
      open_del_user_dialog,
    };
  },
  components: {
    SysCodeInput,
    ConfirmationBox,
  },
};
</script>

<style scoped></style>
